package com.yxq.mdt.app.application;

import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.facade.callback.NavCallback;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.ToastUtils;
import com.yxq.mdt.app.BuildConfig;
import com.yxq.mdt.app.constant.RouteTable;
import com.yxq.mdt.app.utils.WxUtils;

import java.util.ArrayList;

/**
 * @author L
 * @time 2019-10-24
 * @describe 路由工具类$
 */
public class IntentHelper {

    public static void arouter(String url) {
        if (TextUtils.isEmpty(url)) {
            return;
        }
        if (BuildConfig.DEBUG) {
            Log.e("路由", url);
        }
        ARouter.getInstance().build(url).navigation(App.getInstance().getBaseContext(), new NavCallback() {
            @Override
            public void onArrival(Postcard postcard) {

            }

            @Override
            public void onLost(Postcard postcard) {
                super.onLost(postcard);
                //ToastUtils.showShort("找不到对应的页面");
            }
        });
    }

    public static void arouterUri(String url) {
        if (TextUtils.isEmpty(url)) {
            return;
        }
        if (BuildConfig.DEBUG) {
            Log.e("路由", "Uri:" + RouteTable.AROUTER_SCHEME + url);
        }
        ARouter.getInstance().build(Uri.parse(RouteTable.AROUTER_SCHEME + url)).navigation(App.getInstance().getBaseContext(), new NavCallback() {
            @Override
            public void onArrival(Postcard postcard) {

            }

            @Override
            public void onLost(Postcard postcard) {
                super.onLost(postcard);
                //ToastUtils.showShort("找不到对应的页面");
            }
        });
    }

    public static void arouterUri(String url, Bundle bundle) {
        if (TextUtils.isEmpty(url)) {
            return;
        }
        if (BuildConfig.DEBUG) {
            Log.e("路由", "Uri:" + RouteTable.AROUTER_SCHEME + url);
        }
        ARouter.getInstance().build(Uri.parse(RouteTable.AROUTER_SCHEME + url)).with(bundle).navigation(App.getInstance().getBaseContext(), new NavCallback() {
            @Override
            public void onArrival(Postcard postcard) {

            }

            @Override
            public void onLost(Postcard postcard) {
                super.onLost(postcard);
                //ToastUtils.showShort("找不到对应的页面");
            }
        });
    }

    public static void arouter(String url, Bundle bundle) {
        if (TextUtils.isEmpty(url)) {
            return;
        }
        if (BuildConfig.DEBUG) {
            Log.e("路由", "url:" + url);
        }
        ARouter.getInstance().build(url).with(bundle).navigation(App.getInstance().getBaseContext(), new NavCallback() {
            @Override
            public void onArrival(Postcard postcard) {

            }

            @Override
            public void onLost(Postcard postcard) {
                super.onLost(postcard);
                //ToastUtils.showShort("找不到对应的页面");
            }
        });
    }

    /**
     * 跳转查看大图
     *
     * @param position 选中图片下标
     * @param images   图片链接集合
     */
    public static void picture(int position, ArrayList<String> images) {

        ARouter.getInstance().build(RouteTable.ROUTER_ACTIVITY_PICTURE).withInt("position", position).withStringArrayList("images", images).navigation();
    }

    /**
     * 微信链接 跳转页面处理
     *
     * @param skipState 等于1 能跳转小程序
     * @param url
     * @param bundle
     */
    public static void aRouterUriWx(int skipState, String url, Bundle bundle) {
        if (TextUtils.isEmpty(url)) {
            return;
        }
        //优先跳转APP
        ARouter.getInstance().build(Uri.parse(RouteTable.AROUTER_SCHEME + url)).with(bundle).navigation(App.getInstance().getBaseContext(), new NavCallback() {
            @Override
            public void onArrival(Postcard postcard) {

            }

            @Override
            public void onLost(Postcard postcard) {
                super.onLost(postcard);
                //找不到APP页面，则跳转小程序
                if (skipState == 1) {
                    if (!UmengShareHelper.isWeChatAvailable(App.getInstance())) {
                        ToastUtils.showShort("需要跳转微信小程序，请先安装微信");
                        return;
                    }
                    WxUtils.IntentWxSmallApp(App.getInstance(), url);
                } else {
                    //ToastUtils.showShort("找不到对应的页面");
                }
            }
        });
        return;

    }
}
